// --------------------------------------------------------------------------------------------------------------------
// <copyright file="TraceControlExtensions.cs" company="AzureContrib (Open source library http://azurecontrib.codeplex.com)">
//   Microsoft Public License (Ms-PL)
// </copyright>
// <summary>
//   Extension methods on <see cref="ITraceSwitchProvider"/>.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

using System.Diagnostics.CodeAnalysis;

namespace AzureContrib.WindowsAzure.Diagnostics
{
	/// <summary>
	/// Extension methods on <see cref="ITraceSwitchProvider"/>.
	/// </summary>
	public static class TraceControlExtensions
	{
		/// <summary>
		/// Is tracing set to <see cref="LogLevel.Off"/> for this <see cref="ITraceSwitchable"/>?
		/// </summary>
		/// <param name="traceSwitchProvider">The <see cref="ITraceSwitchProvider"/> to check.</param>
		/// <param name="traceSwitchable">A <see cref="ITraceSwitchable"/> instance.</param>
		/// <returns>True if tracing is enabled.</returns>
		[SuppressMessage("Microsoft.Design", "CA1062:Validate arguments of public methods", MessageId = "0", Justification = "AzureContrib.WindowsAzure.Core.Assumes.NotNull handles null checking.")]
		[SuppressMessage("Microsoft.Design", "CA1062:Validate arguments of public methods", MessageId = "1", Justification = "AzureContrib.WindowsAzure.Core.Assumes.NotNull handles null checking.")]
		public static bool Disabled(this ITraceSwitchProvider traceSwitchProvider, ITraceSwitchable traceSwitchable)
		{
			Assumes.NotNull(traceSwitchProvider, "traceSwitchProvider");
			Assumes.NotNull(traceSwitchable, "traceSwitchable");
			return traceSwitchProvider.GetSwitch(traceSwitchable).Level == LogLevel.Off;
		}

		/// <summary>
		/// Is tracing at the <see cref="LogLevel.Verbose"/> level enabled for this <see cref="ITraceSwitchable"/>?
		/// </summary>
		/// <param name="traceSwitchProvider">The <see cref="ITraceSwitchProvider"/> to check.</param>
		/// <param name="traceSwitchable">A <see cref="ITraceSwitchable"/> instance.</param>
		/// <returns>True if tracing is enabled.</returns>
		[SuppressMessage("Microsoft.Design", "CA1062:Validate arguments of public methods", MessageId = "0", Justification = "AzureContrib.WindowsAzure.Core.Assumes.NotNull handles null checking.")]
		[SuppressMessage("Microsoft.Design", "CA1062:Validate arguments of public methods", MessageId = "1", Justification = "AzureContrib.WindowsAzure.Core.Assumes.NotNull handles null checking.")]
		public static bool VerboseEnabled(this ITraceSwitchProvider traceSwitchProvider, ITraceSwitchable traceSwitchable)
		{
			Assumes.NotNull(traceSwitchProvider, "traceSwitchProvider");
			Assumes.NotNull(traceSwitchable, "traceSwitchable");
			return traceSwitchProvider.GetSwitch(traceSwitchable).TraceVerbose;
		}

		/// <summary>
		/// Is tracing at the <see cref="LogLevel.Information"/> level enabled for this <see cref="ITraceSwitchable"/>?
		/// </summary>
		/// <param name="traceSwitchProvider">The <see cref="ITraceSwitchProvider"/> to check.</param>
		/// <param name="traceSwitchable">A <see cref="ITraceSwitchable"/> instance.</param>
		/// <returns>True if tracing is enabled.</returns>
		[SuppressMessage("Microsoft.Design", "CA1062:Validate arguments of public methods", MessageId = "0", Justification = "AzureContrib.WindowsAzure.Core.Assumes.NotNull handles null checking.")]
		[SuppressMessage("Microsoft.Design", "CA1062:Validate arguments of public methods", MessageId = "1", Justification = "AzureContrib.WindowsAzure.Core.Assumes.NotNull handles null checking.")]
		public static bool InformationEnabled(this ITraceSwitchProvider traceSwitchProvider, ITraceSwitchable traceSwitchable)
		{
			Assumes.NotNull(traceSwitchProvider, "traceSwitchProvider");
			Assumes.NotNull(traceSwitchable, "traceSwitchable");
			var traceSwitch = traceSwitchProvider.GetSwitch(traceSwitchable);
			return traceSwitch.TraceInformation;
		}

		/// <summary>
		/// Is tracing at the <see cref="LogLevel.Warning"/> level enabled for this <see cref="ITraceSwitchable"/>?
		/// </summary>
		/// <param name="traceSwitchProvider">The <see cref="ITraceSwitchProvider"/> to check.</param>
		/// <param name="traceSwitchable">A <see cref="ITraceSwitchable"/> instance.</param>
		/// <returns>True if tracing is enabled.</returns>
		[SuppressMessage("Microsoft.Design", "CA1062:Validate arguments of public methods", MessageId = "0", Justification = "AzureContrib.WindowsAzure.Core.Assumes.NotNull handles null checking.")]
		[SuppressMessage("Microsoft.Design", "CA1062:Validate arguments of public methods", MessageId = "1", Justification = "AzureContrib.WindowsAzure.Core.Assumes.NotNull handles null checking.")]
		public static bool WarningEnabled(this ITraceSwitchProvider traceSwitchProvider, ITraceSwitchable traceSwitchable)
		{
			Assumes.NotNull(traceSwitchProvider, "traceSwitchProvider");
			Assumes.NotNull(traceSwitchable, "traceSwitchable");
			return traceSwitchProvider.GetSwitch(traceSwitchable).TraceWarning;
		}

		/// <summary>
		/// Is tracing at the <see cref="LogLevel.Error"/> level enabled for this <see cref="ITraceSwitchable"/>?
		/// </summary>
		/// <param name="traceSwitchProvider">The <see cref="ITraceSwitchProvider"/> to check.</param>
		/// <param name="traceSwitchable">A <see cref="ITraceSwitchable"/> instance.</param>
		/// <returns>True if tracing is enabled.</returns>
		[SuppressMessage("Microsoft.Design", "CA1062:Validate arguments of public methods", MessageId = "0", Justification = "AzureContrib.WindowsAzure.Core.Assumes.NotNull handles null checking.")]
		[SuppressMessage("Microsoft.Design", "CA1062:Validate arguments of public methods", MessageId = "1", Justification = "AzureContrib.WindowsAzure.Core.Assumes.NotNull handles null checking.")]
		public static bool ErrorEnabled(this ITraceSwitchProvider traceSwitchProvider, ITraceSwitchable traceSwitchable)
		{
			Assumes.NotNull(traceSwitchProvider, "traceSwitchProvider");
			Assumes.NotNull(traceSwitchable, "traceSwitchable");
			return traceSwitchProvider.GetSwitch(traceSwitchable).TraceError;
		}

		/// <summary>
		/// Is tracing at the <see cref="LogLevel.Critical"/> level enabled for this <see cref="ITraceSwitchable"/>?
		/// </summary>
		/// <param name="traceSwitchProvider">The <see cref="ITraceSwitchProvider"/> to check.</param>
		/// <param name="traceSwitchable">A <see cref="ITraceSwitchable"/> instance.</param>
		/// <returns>True if tracing is enabled.</returns>
		[SuppressMessage("Microsoft.Design", "CA1062:Validate arguments of public methods", MessageId = "0", Justification = "AzureContrib.WindowsAzure.Core.Assumes.NotNull handles null checking.")]
		[SuppressMessage("Microsoft.Design", "CA1062:Validate arguments of public methods", MessageId = "1", Justification = "AzureContrib.WindowsAzure.Core.Assumes.NotNull handles null checking.")]
		public static bool CriticalEnabled(this ITraceSwitchProvider traceSwitchProvider, ITraceSwitchable traceSwitchable)
		{
			Assumes.NotNull(traceSwitchProvider, "traceSwitchProvider");
			Assumes.NotNull(traceSwitchable, "traceSwitchable");
			return traceSwitchProvider.GetSwitch(traceSwitchable).TraceCritical;
		}
	}
}